// ESLint v9+ 扁平配置
import eslint from '@eslint/js'
import pluginVue from 'eslint-plugin-vue'
import tseslint from 'typescript-eslint'
import prettierPlugin from 'eslint-plugin-prettier'
import prettierConfig from 'eslint-config-prettier'
import globals from 'globals'
import vueParser from 'vue-eslint-parser'
import tsParser from '@typescript-eslint/parser'

export default [
  // 忽略配置
  {
    ignores: [
      'node_modules/',
      'dist/',
      'dist-test/',
      'coverage/',
      '*.d.ts',
      '*.md',
      'mobile/**',
      'scripts/**',
      'public/static/**',
      'coverage/**',
      '**/*.backup.*',
      '**/*.config.backup.*',
    ],
  },

  // 基础配置
  eslint.configs.recommended,

  // Prettier配置
  prettierConfig,

  // TypeScript配置
  ...tseslint.configs.recommended,

  // Vue配置
  ...pluginVue.configs['flat/recommended'],

  // 全局规则
  {
    plugins: {
      prettier: prettierPlugin,
    },
    languageOptions: {
      ecmaVersion: 2022,
      sourceType: 'module',
      globals: {
        ...globals.browser,
        ...globals.node,
        defineProps: 'readonly',
        defineEmits: 'readonly',
        defineExpose: 'readonly',
        withDefaults: 'readonly',
        process: 'readonly',
        module: 'readonly',
        console: 'readonly',
        window: 'readonly',
        document: 'readonly',
        navigator: 'readonly',
        localStorage: 'readonly',
        sessionStorage: 'readonly',
        setInterval: 'readonly',
        clearInterval: 'readonly',
        setTimeout: 'readonly',
        clearTimeout: 'readonly',
        location: 'readonly',
        history: 'readonly',
        initWechatSDK: 'readonly',
        wechatAuthLogin: 'readonly',
        createWebHashHistory: 'readonly',
        config: 'readonly',
      },
    },
    rules: {
      // 基础规则
      'no-console': process.env.NODE_ENV === 'production' ? 'warn' : 'off',
      'no-debugger': process.env.NODE_ENV === 'production' ? 'warn' : 'off',

      // Vue规则
      'vue/multi-word-component-names': 'off',
      'vue/no-v-html': 'off',
      'vue/require-default-prop': 'off',
      'vue/require-prop-types': 'off',
      'vue/no-unused-components': 'warn',
      'vue/max-attributes-per-line': 'off',
      'vue/html-self-closing': 'off',
      'vue/first-attribute-linebreak': 'off',
      'vue/html-closing-bracket-newline': 'off',
      'vue/html-indent': 'off',
      'vue/attributes-order': 'off',

      // TypeScript规则
      '@typescript-eslint/no-explicit-any': 'off',
      '@typescript-eslint/no-empty-object-type': 'off',
      '@typescript-eslint/no-unused-vars': 'off',
      '@typescript-eslint/no-var-requires': 'off',
      '@typescript-eslint/explicit-function-return-type': 'off',
      '@typescript-eslint/no-unsafe-member-access': 'off',
      '@typescript-eslint/no-unsafe-call': 'off',
      '@typescript-eslint/no-unsafe-assignment': 'off',
      '@typescript-eslint/no-unsafe-return': 'off',
      '@typescript-eslint/no-unsafe-argument': 'off',
      '@typescript-eslint/restrict-template-expressions': 'off',
      '@typescript-eslint/restrict-plus-operands': 'off',

      // 通用规则
      'no-case-declarations': 'off',
      'no-useless-catch': 'off',

      // Prettier规则
      'prettier/prettier': 'off',
    },
  },

  // TypeScript文件配置
  {
    files: ['*.ts', '*.tsx'],
    languageOptions: {
      parser: tsParser,
      parserOptions: {
        ecmaVersion: 2022,
        sourceType: 'module',
      },
    },
    rules: {
      'no-undef': 'off',
    },
  },

  // Vue文件配置 - 临时禁用严格检查
  {
    files: ['*.vue'],
    languageOptions: {
      parser: vueParser,
      parserOptions: {
        ecmaVersion: 2022,
        sourceType: 'module',
        extraFileExtensions: ['.vue'],
        parser: tsParser,
        allowImportExportEverywhere: true,
      },
    },
    rules: {
      'vue/no-unused-vars': 'off',
      'vue/script-setup-uses-vars': 'off',
      'no-undef': 'off',
      'no-unused-vars': 'off',
      'vue/component-name-in-template-casing': 'off',
      'vue/prop-name-casing': 'off',
      '@typescript-eslint/no-unused-vars': 'off',
      '@typescript-eslint/no-explicit-any': 'off',
      'no-unused-expressions': 'off',
      'prefer-const': 'off',
      'no-undef-init': 'off',
      'no-dupe-class-members': 'off',
      'no-duplicate-imports': 'off',
    },
  },
]
